package com.mgq.juc.cas;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeAccessor {
    private static Unsafe unsafe;

    public static Unsafe getUnsafe() {
        return unsafe;
    }

    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            //字段是static的,不是对象的,所以传入null
            //get()方法返回的是这个字段的值
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new Error(e);
        }


    }
}
